package logic

import (
	"gameserver/tools/TestRobot/cmd"
	"gameserver/tools/TestRobot/models"
	"time"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/logger"
)

func init() {
	cmd.GetCmdMgr().RegisterLogic(cmd.Logic_Login, "登录账号与角色", (*LogicLogin)(nil))
}

const (
	LgStep_ReStart StepTypeEnum = iota
	LgStep_GateWay
	LgStep_end
)

type LogicLogin struct {
	LogicBase
	register *LogicRegister
	gateway  *LogicGateWay
	Delay    time.Duration
}

func (lg *LogicLogin) String() string {
	sb := common.NewStringBuilder()
	sb.Appendln(lg.LogicBase.String())
	sb.Appendln(lg.register.String())
	sb.Appendln(lg.gateway.String())
	return sb.ToString()
}

// 生成命令参数
func (this *LogicLogin) GenCmdParam(cmd *cmd.CmdInfo) {
	cmd.AddParam("delay", "延迟多少时间开始登录", "0")
}

// 解析命令 成功继续，失败显示命令参数 这里不能设置给this
func (this *LogicLogin) ParseCmd(cmd *cmd.InputCmd) bool {

	return true
}

// 销毁逻辑
func (this *LogicLogin) Destroy() {
	this.register.Destroy()
	this.gateway.Destroy()
}
func (this *LogicLogin) Init(user *models.RobotUser) {
	logger.Infof("LogicLogin start:%s", user.UserName)
	this.LogicBase.Init(user)
	this.LogicBase.realPtr = this
	this.maxStep = LgStep_end
	this.register = new(LogicRegister)
	this.gateway = new(LogicGateWay)
	ic := this.currIC

	this.register.Init(user)
	this.gateway.Init(user)
	this.Delay = time.Duration(ic.GetInt64("delay"))

	this.stepLi[LgStep_ReStart] = this.registerRun
	this.stepLi[LgStep_GateWay] = this.gatewayRun
	this.SetTimer(this.Delay)

}

// 类型 名字
func (this *LogicLogin) GetName() string {
	return "LogicLogin"
}

func (lg *LogicLogin) registerRun() {
	if lg.register.IsEnd() {
		lg.SetNext(LgStep_ReStart+1, 0)
	} else {
		lg.register.Run()
	}
}

func (lg *LogicLogin) gatewayRun() {
	if lg.gateway.IsEnd() {
		lg.SetNext(LgStep_GateWay+1, 0)
	} else {
		lg.gateway.Run()
	}

}
